<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="节点名称" prop="bqdjmc">
				<el-input v-model="dataForm.bqdjmc" placeholder="名称"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" :loading="loading" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { updateLabelTreeNode } from '@/api/labels-mana/index'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	sjbqid: '',
	bqdjmc: '',
	bqdj: '0',
	id: ''
	/* disabledProjectId: false */
})

const loading = ref(false)

const init = (id?: any, parentId: any, bqdj: any, name: string) => {
	loading.value = false
	visible.value = true

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	dataForm.id = ''
	dataForm.sjbqid = parentId
	dataForm.bqdjmc = name
	/* if(projectId) {
		dataForm.disabledProjectId = true
		dataForm.projectId = projectId
	} else {
		dataForm.disabledProjectId = false
		dataForm.projectId = ''
	} */

	if (id) {
		dataForm.id = id
		dataForm.bqdj = bqdj
	} else {
		dataForm.bqdj = Number(bqdj) + 1 + ''
	}
}

const dataRules = ref({
	bqdjmc: [{ required: true, pattern: '[^ \x22]+', message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			loading.value = false
			return false
		}
		loading.value = true

		updateLabelTreeNode(dataForm)
			.then(() => {
				ElMessage.success({
					message: '操作成功',
					duration: 500,
					onClose: () => {
						loading.value = false
						visible.value = false
						emit('refreshDataList')
					}
				})
			})
			.catch(() => {
				loading.value = false
			})
	})
}

defineExpose({
	init
})
</script>
